package djboom.materials.display.devices {
	import com.greensock.easing.Linear;
	import djboom.materials.events.BottleOfWaterEvent;
	import flash.events.MouseEvent;

	import com.greensock.TweenLite;

	import flash.display.MovieClip;

	import djboom.materials.display.elements.HotSpot;
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.display.elements.SimpleSmartSprite;

	import org.casalib.display.CasaBitmap;

	import flash.events.Event;

	/**
	 * Склянка с дистиллированной водой.
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 01.02.2011
	 * 
	 */

	public class BottleOfWater extends SimpleSmartSprite {

		
		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const BACK:String = "bottleOfWater.Back";		private static const BORDER:String = "bottleOfWater.Border";		private static const ON:String = "bottleOfWater.On";		private static const WATER:String = "bottleOfWater.Water";		private static const WATER_MASK:String = "bottleOfWater.WaterMask";
		
		public static const TIMER_OPEN_WATER:int = 3;

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _backBitmap:CasaBitmap;
		private var _switchHS:HotSpot;
		private var _waterBitmap:CasaBitmap;
		private var _waterMask:MovieClip;
		private var _borderBitmap:CasaBitmap;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function BottleOfWater() {
			super();
			
			_backBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BACK));
			addChild(_backBitmap);
			
			_switchHS = new HotSpot();
			_switchHS.drawBitmap(BitmapDataCore.getBitmapData(ON));
			_switchHS.x = 163;			_switchHS.y = 103;
			addChild(_switchHS);
            
			_waterBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(WATER));
			_waterBitmap.x = 35;			_waterBitmap.y = 24;
			addChild(_waterBitmap);
			
			_waterMask = BitmapDataCore.getMovieClip(WATER_MASK);
			_waterMask.x = 35;
			addChild(_waterMask);
            
			_waterBitmap.mask = _waterMask;
            
			_borderBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BORDER));
			_borderBitmap.x = 35;
			addChild(_borderBitmap);
		}	 

		// ----------------------------------------------------------------------------
		// Приватные методы класса.
		// ----------------------------------------------------------------------------
	 		
		// ----------------------------------------------------------------------------
		// Видимые методы класса.
		// ----------------------------------------------------------------------------
	 	
		// ----------------------------------------------------------------------------
		// Переопределенные методы.
		// ----------------------------------------------------------------------------

		override public function clearContent(event:Event):void {
			super.clearContent(event);
		}

		// ----------------------------------------------------------------------------
		// Обработчики событий.
		// ----------------------------------------------------------------------------

		private function enableBottle(event:MouseEvent):void {
			_switchHS.clearBitmap();
			_switchHS.removeEventListener(MouseEvent.CLICK, enableBottle);
			
			TweenLite.to(_waterBitmap, TIMER_OPEN_WATER, {y: "20", ease: Linear.easeNone, onStart: startOpenWater, onComplete: completeOpenWater});
		}

		private function startOpenWater():void {
			dispatchEvent(new BottleOfWaterEvent(BottleOfWaterEvent.OPEN_WATER));
		}

		private function completeOpenWater():void {
			_switchHS.destroy();
            _switchHS = null;
            
            _switchHS = new HotSpot();
            _switchHS.drawBitmap(BitmapDataCore.getBitmapData(ON));
            _switchHS.x = 163;
            _switchHS.y = 103;
            addChild(_switchHS);
		}
		
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function enable():void {
			_switchHS.addEventListener(MouseEvent.CLICK, enableBottle);
		}

		
	 
	 		// ----------------------------------------------------------------------------
	 		// Методы доступа.
	 		// ----------------------------------------------------------------------------
	}
}